﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Text;
using System.Web.Script.Serialization;
using Assignment_3.Classes;

namespace Assignment_3.Feature___Reports
{
    public partial class OverviewReport : System.Web.UI.Page
    {
        #region Control Methods
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["username"] == null || Session["usertype"] == null)
            {
                Response.Redirect("~/Feature - Users/Login.aspx");
            }

            chartPanel.Visible = false;
            dateTbx.Attributes.Add("ReadOnly", "true");
            if (!IsPostBack)
            {
                //Show records for the today's date
                DateTime currentdate = DateTime.Now;
                dateTbx.Text = currentdate.ToString("yyyy-MM-dd");
                List<Overview> overviewList = new List<Overview>();
                overviewList = DatabaseManager.getOverviewReport(currentdate.ToShortDateString());
                if (overviewList.Count != 0)
                {
                    chartPanel.Visible = true;
                }
                else
                {
                    emptyLbl.Text = "No record for today.";
                    chartPanel.Visible = false;
                }
                StringBuilder data = new StringBuilder();
                JavaScriptSerializer json = new JavaScriptSerializer();
                json.Serialize(overviewList, data);
                hdData.Value = data.ToString();
            }
        }

        protected void dateTbx_TextChanged(object sender, System.EventArgs e)
        {
            if (Page.IsValid)
            {
                DateTime date;
                //Check for correct date format
                bool checkdate = DateTime.TryParse(dateTbx.Text, out date);
                List<Overview> overviewList = new List<Overview>();
                emptyLbl.Text = "";
                if (checkdate == true)
                {
                    overviewList = DatabaseManager.getOverviewReport(dateTbx.Text);

                    //Check if there are any records
                    if (overviewList.Count != 0)
                    {
                        chartPanel.Visible = true;
                    }
                    else
                    {
                        emptyLbl.Text = "No record for this date";
                        chartPanel.Visible = false;
                    }

                    //Draw Table Chart
                    StringBuilder data = new StringBuilder();
                    JavaScriptSerializer json = new JavaScriptSerializer();
                    json.Serialize(overviewList, data);
                    hdData.Value = data.ToString();
                }
                else
                {
                    emptyLbl.Text = "Please write a proper date format!";
                }
            }
        }
        #endregion 
    }
}